package com.tsingteng.cosfun.ui.costar;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.tsingteng.cosfun.R;
import com.tsingteng.cosfun.base.BaseFragment;
import com.tsingteng.cosfun.bean.VideoLocalBean;
import com.tsingteng.cosfun.helper.VideoHelper;
import com.tsingteng.cosfun.utils.ImageUtils;
import com.tsingteng.cosfun.utils.StringUtils;
import com.tsingteng.cosfun.widget.MyRecycleView;
import com.tsingteng.cosfun.widget.TitleView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.Unbinder;

/**
 * 描述 :
 * 创建人: 肖利.
 * 时间:2018/10/18
 * qq(872179966)
 */

public class VideoLocalFragment extends BaseFragment {

    @BindView(R.id.tv_title)
    TitleView tvTitle;
    @BindView(R.id.rv_rv)
    RecyclerView rvRv;
    private List<VideoLocalBean> localVideoList = new ArrayList<>();
    private VideoLocalAdapter videoLocalAdapter;
    private GridLayoutManager mLayoutManager;
    private int page = 0;
    private boolean isFinishLoad = false;


    private List<VideoLocalBean> currentLocalVideoList;
    private TextView mTvFooter;

    public static VideoLocalFragment newInstance() {
        VideoLocalFragment fragment = new VideoLocalFragment();
        return fragment;
    }

    @Override
    protected void initView() {
        tvTitle.setTitle("选择视频");
        tvTitle.setIvLeft(R.drawable.close_icon);
        tvTitle.setOnTitleClickListener(new TitleView.OnTitleClickListener() {
            @Override
            public void onLeft() {
                getActivity().finish();
            }

            @Override
            public void onRight() {

            }

            @Override
            public void onRight2() {

            }
        });
        VideoHelper.getIntence().getLocalVideoList(getActivity());
        mLayoutManager = new GridLayoutManager(getContext(), 3, GridLayoutManager.VERTICAL, false);
        rvRv.setLayoutManager(mLayoutManager);
        videoLocalAdapter = new VideoLocalAdapter();
        rvRv.setAdapter(videoLocalAdapter);
        videoLocalAdapter.setEnableLoadMore(false);
//        videoLocalAdapter.addData(list);
        videoLocalAdapter.setFooterView(getFooterView());
        videoLocalAdapter.getFooterLayout().setVisibility(View.GONE);
        page = 0;
        VideoHelper.getIntence().setOnLocalVideoListener(new VideoHelper.OnLocalVideoListener() {
            @Override
            public void getVideoInfo30(List<VideoLocalBean> list) {
                if (localVideoList == null) localVideoList = new ArrayList<>();
                localVideoList.clear();
                localVideoList.addAll(list);
                videoLocalAdapter.addData(getCutrrentList(page));
                videoLocalAdapter.getFooterLayout().setVisibility(View.VISIBLE);
            }


            @Override
            public void getVideoInfo60(List<VideoLocalBean> list) {
                if (localVideoList == null) localVideoList = new ArrayList<>();
                localVideoList.clear();
                localVideoList.addAll(list);
            }

            @Override
            public void getVideoInfoAll(List<VideoLocalBean> list) {
                if (localVideoList == null) localVideoList = new ArrayList<>();
                localVideoList.clear();
                localVideoList.addAll(list);
                isFinishLoad = true;

            }
        });

        mTvFooter.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                page++;
                List<VideoLocalBean> cutrrentList = getCutrrentList(page);
                if (cutrrentList == null || cutrrentList.size()<=0) {
                    page--;
                    if (isFinishLoad) {
                        mTvFooter.setText("已无更多数据!");
                        mTvFooter.setEnabled(false);
                    }
                    return;
                }
                videoLocalAdapter.addData(cutrrentList);
                videoLocalAdapter.notifyDataSetChanged();
            }
        });


    }


    public View getFooterView() {
        View footerView = View.inflate(getContext(), R.layout.footer_video_local_view, null);
        mTvFooter = footerView.findViewById(R.id.tv_footer);
        return footerView;
    }


    public List<VideoLocalBean> getCutrrentList(int page) {
        if (currentLocalVideoList == null) currentLocalVideoList = new ArrayList<>();
        currentLocalVideoList.clear();
        int starPosition = 15 * page;
        int endPosition = starPosition + 15;
        if (starPosition >= localVideoList.size()) return null;
        if (endPosition >= localVideoList.size()) endPosition = localVideoList.size() - 1;
        if (starPosition > endPosition) return null;
        for (int i = starPosition; i < endPosition; i++) {
            currentLocalVideoList.add(localVideoList.get(i));
        }
        return currentLocalVideoList;
    }

    @Override
    protected void getBundle(Bundle bundle) {

    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_video_local;
    }


    class VideoLocalAdapter extends BaseQuickAdapter<VideoLocalBean, BaseViewHolder> {

        public VideoLocalAdapter() {
            super(R.layout.item_local_video_adapter);
        }

        @Override
        protected void convert(BaseViewHolder helper, VideoLocalBean item) {
            ImageView videoCover = helper.getView(R.id.iv_video_cover);
            TextView videoTime = helper.getView(R.id.tv_time);
            ImageUtils.LoadImage(getContext(), item.getVideoCover(), videoCover);
            videoTime.setText((int)(item.getDuration()/1000) + "s");
            videoCover.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (TextUtils.isEmpty(item.getUrl())) {
                        showToast("视频解析错误!");
                        return;
                    }

                    if (item.getDuration()>60000*30) {
                        showToast("视频过大!");
                        return;
                    }
                    Intent intent = new Intent(getActivity(), PreViewActivity.class);
                    intent.putExtra(VideoHelper.VIDEO_PATH, item.getUrl());
                    intent.putExtra(VideoHelper.VIDEO_LOCAL, true);
                    startActivity(intent);
                }
            });
        }
    }


}
